//
//  XMHudProgress.m
//  MotoEye
//
//  Created by 徐盟 on 2023/5/9.
//

#import "XMHudProgress.h"
#import "MBProgressHUD.h"

@interface XMHudProgress()<MBProgressHUDDelegate>

@property(nonatomic, weak) UIViewController *controller;
@property(nonatomic, strong) MBProgressHUD *hud;

@end

@implementation XMHudProgress

- (MBProgressHUD *)hud
{
    if(_hud ==nil){
        _hud = [[MBProgressHUD alloc]initWithView:self.controller.view];
        _hud.removeFromSuperViewOnHide = YES;

    }
    return _hud;
}
-(NSString *)currTag
{
    if(_currTag == nil){
        _currTag = @"defult_tag";
    }
    return _currTag;
}

-(instancetype) initWithController:(UIViewController *) controller
{
    if(self = [super init]){
        self.controller = controller;
    }
    return self;
}

+(instancetype) XMHudWithController:(UIViewController *) controller
{
    return [[self alloc] initWithController:controller];
}

-(void)createLoading
{
//    self.hud = [[MBProgressHUD alloc]initWithView:self.controller.view];
    self.hud.removeFromSuperViewOnHide = YES;
    self.hud.mode = MBProgressHUDModeIndeterminate;
    self.hud.contentColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:1.f];
    self.hud.label.text = @"";
    [self toShow];
}
-(void) createLoadingWithLabel:(NSString *) label
{
//    self.hud = [[MBProgressHUD alloc]initWithView:self.controller.view];
    self.hud.removeFromSuperViewOnHide = YES;
    self.hud.mode = MBProgressHUDModeIndeterminate;
    self.hud.label.text = label;
    self.hud.label.lineBreakMode = NSLineBreakByCharWrapping;
    self.hud.label.numberOfLines = 0;
    self.hud.contentColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:1.f];
    [self toShow];
}

-(void) createTips:(NSString *) label
{
//    self.hud = [[MBProgressHUD alloc]initWithView:self.controller.view];
    self.hud.removeFromSuperViewOnHide = YES;
    self.hud.mode = MBProgressHUDModeText;
    self.hud.label.text = label;
    self.hud.label.lineBreakMode = NSLineBreakByCharWrapping;
    self.hud.label.numberOfLines = 0;
    self.hud.contentColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:1.f];
    [self toShow];
    [self timeoutDismiss:3.0];
}
-(void) createAlert:(NSString *) label
{
//    self.hud = [[MBProgressHUD alloc]initWithView:self.controller.view];
    self.hud.removeFromSuperViewOnHide = YES;
    self.hud.mode = MBProgressHUDModeText;
    self.hud.label.text = label;
    self.hud.label.lineBreakMode = NSLineBreakByCharWrapping;
    self.hud.label.numberOfLines = 0;
//    self.hud.contentColor = [UIColor colorWithRed:1.f green:0.1f blue:0.1f alpha:1.f];
    [self toShow];
    [self timeoutDismiss:3.0];
}


-(void) toShow
{
    if(self.hud && self.controller){
        [self.controller.view addSubview:self.hud];
        [self.hud showAnimated:YES];
        [self timeoutDismiss:30];
    }
}

-(void) toHide
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if(self.hud){
            [self.hud hideAnimated:YES];
        }
        
    });
}
-(void) timeoutDismiss:(CGFloat) time
{
    [self.hud hideAnimated:YES afterDelay:time];
}

-(XMHudProgress *(^)(CGFloat))timeout
{
    return ^XMHudProgress *(CGFloat time){
        [self timeoutDismiss:time];
        return self;
    };
}

-(XMHudProgress *(^)())loading
{
    return ^XMHudProgress *(){
        [self createLoading];
        return self;
    };
}

-(XMHudProgress *(^)(NSString* _Nullable))loadingWithLable
{
    return ^XMHudProgress *(NSString * _Nullable label){
        [self createLoadingWithLabel:label];
        return self;
    };
}
-(XMHudProgress *(^)(NSString* _Nullable))tips
{
    return ^XMHudProgress *(NSString * _Nullable label){
        [self createTips:label];
        return self;
    };
}
-(XMHudProgress *(^)(NSString* _Nullable))alert
{
    return ^XMHudProgress *(NSString * _Nullable label){
        [self createAlert:label];
        return self;
    };
}

-(XMHudProgress* (^)(NSString * _Nullable))tag
{
    return ^XMHudProgress*(NSString * tag){
        self.currTag = tag;
        return self;
    };
}


-(void(^)(NSString * _Nullable))hideWithTag
{
    return ^(NSString * _Nullable tag){
        if([tag isEqualToString:self.currTag]){
            [self toHide];
        }
    };
}

-(void(^)())hide
{
    return ^(){
        [self toHide];
    };
}


@end
